package mzt;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.apache.commons.io.FileUtils;
import org.jsoup.Connection;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Document.OutputSettings;

public class Demo3 {
	public static void main(String[] args) throws Exception {
		// https://blog.csdn.net/lf_breeze/article/details/51862107
		// https://stackoverflow.com/questions/13558448/jsoup-http-error-fetching-url
		// https://blog.csdn.net/u013123635/article/details/78447440
		String src = "http://img.mmjpg.com/2018/1305/1in1.jpg";
		URL url = new URL(src);
		URLConnection con = url.openConnection();
		con.setConnectTimeout(5 * 1000);
		String referer = url.getProtocol() + "://" + url.getHost();
		con.setRequestProperty("Referer", referer);
		System.out.println(referer);
		FileUtils.copyInputStreamToFile(con.getInputStream(), new File("c://2.jpg"));

		//
		Connection ref = Jsoup.connect(url.toString()).ignoreContentType(true).referrer(referer);
		Response execute = ref.execute();
		BufferedInputStream bodyStream = execute.bodyStream();
		FileUtils.copyInputStreamToFile(bodyStream, new File("c://1.jpg"));
	}
}
